// pages/imgupload/imgupload.js
const app = getApp();
Page({

    data: {
        setting: {
            iscut: false,//是否裁剪
            cuttype:0,//裁剪图片类型,0,1:1比例类型,1,5:3比例类型
            maxcount: 9,//最大上传个数
            param: {},//调用上传接口所需参数

        },//功能设置
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var _this = this;
        var setting = JSON.parse(options.setting);
        _this.setData({
            setting: setting
        });
        _this.setData({
            "setting.iscut":  setting.iscut,
            "setting.maxcount": setting.maxcount ? setting.maxcount : _this.data.setting.maxcount
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
    ,
    //选择获取图片源
    ChooseSource: function (e) {
        var _this = this;
        var dataset = e.currentTarget.dataset;
        _this.setData({
            "setting.sourceType": dataset.source
        })
        _this.ImgsUp();
    },
    ImgsUp: function () {
        var _this = this;
        var setting = _this.data.setting;
        var maxcount = setting.maxcount;
        var sourceType = setting.sourceType;
        var param = setting.param;
        let pages = getCurrentPages();//当前页面
        let prevPage = pages[pages.length - 2];//上一页面
        wx.chooseImage({
            count: maxcount,
            sizeType: ['compressed'],
            sourceType: [sourceType],
            success: function (res) {
                var iscut=setting.iscut;
                iscut = maxcount > 1 ? false : iscut;
                var tempFilePaths = res.tempFilePaths;
                if(iscut){
                    wx.navigateTo({
                        url: '../imgupload/imgCut?setting=' + JSON.stringify(setting) + "&imgfile=" + tempFilePaths[0]
                    })
                    return;
                }
                var pathArry = new Array;
                var isboole = true;
                wx.showLoading({
                    title: '正在上传中...'
                })
                var completeCount = 0;
                for (var i = 0; i < tempFilePaths.length; i++) {
                    wx.uploadFile({
                        url: app.globalData.UploadUrl,
                        filePath: tempFilePaths[i],
                        name: 'file',
                        formData: param,
                        success: function (res) {
                            var result = JSON.parse(res.data);
                            if (result.code == 1) {
                                pathArry.push(result.data);
                                app.globalData.FilePaths = pathArry;
                                prevPage.setData({
                                    FilePaths: pathArry
                                });
                            }
                        },
                        complete: function () {
                            completeCount++;
                            if (completeCount == tempFilePaths.length) {
                                wx.hideLoading();
                                wx.navigateBack({
                                    delta: 1
                                })
                            }
                        }
                    })
                }
               
            }
        })
    },
})